#include<iostream>
using namespace std;

class Shape
{
protected:
    int width, height;
public:
    Shape(int a = 0, int b=0) {
        width = a;
        height = b;
    }
    ~Shape() {}

    int area(){
        cout << "parent class area:" <<endl;
        return 0;
    }
};

class Rectangle: public Shape
{

public:
    Rectangle(int a = 0, int b= 0):Shape(a, b) {}
    ~Rectangle() {}

    int area(){
        cout << "rectangle class area:" << endl;
        return (width * height);
    }
};

class Triangle:public Shape
{
public:
    Triangle(int a = 0, int b = 0):Shape(a, b) {}
    ~Triangle() {}

    int area(){
        cout << "triangle class area:" << endl;
        return (width * height / 2);
    }
};

int main(){
    //普通情况使用
    Rectangle r1(10, 7);
    cout << r1.area() << endl;//输出70

    Shape *shape;
    Rectangle rec(10, 7);
    Triangle tri(10, 5);

    shape = &rec;
    cout << shape->area() << endl;//矩形的面积 输出0

    shape = &tri;
    cout << shape->area() << endl;//三角形面积 输出0
    //上面两个例子输出0是因为”早绑定“，area()函数在编译期间就设置好了，为基类的函数
    //如果在Shapre的area函数前加上virtual，意思就是告诉编译器不要"早绑定"，要"后期绑定"，也就是调用各个派生类的函数
    //这样的area()函数叫做虚函数


    //纯虚函数
    //如果area()没有函数体，实现全部在派生类，则area()叫做纯虚函数
    return 0;
}